// SendThread.cpp: implementation of the CSendThread class.
//
//////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "SendThread.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSendThread::CSendThread(IUdxTcp* pTcp)
{
	m_pThread = CreateUdxThread();
	m_pThread->SetCB(this);
	pTcp->AddLife();

	pTcp->SetUserData((INT64)this);
	m_pTcp = pTcp;


	m_pThread->Start();

}

CSendThread::~CSendThread()
{
	Stop();
}
extern void UpdateSpeed( IUdxTcp * pSocket);

void CSendThread::UdxRun()
{
	const int len = 1024*10;
	BYTE senddata[len];

	IUdxTime * pTime = CreateUdxTime();

	DWORD start = pTime->GetTickCount();

	while (m_pTcp->IsConnected())
	{
		if(!m_pTcp->SendBuff(senddata,len))
		{
			m_pTcp->GetWaitEvent()->Wait(10);
		}

		if(pTime->GetTickCount() - start > 1000)
		{
			start = pTime->GetTickCount();
			UpdateSpeed(m_pTcp);
		}
	}
}

void CSendThread::Stop()
{
	if(m_pThread)
	{

		m_pThread->Destroy();
		m_pThread = NULL;
		m_pTcp->Destroy();
		m_pTcp = NULL;
	}
}
